#include <rtthread.h>
#include <rtdevice.h>
#include "../src/lvgl.h"
#include "lv_port_rtt.h"
#include "lv_demo_widgets.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

void lvgl_tick(void *param)
{
    int cnt = 0;
    while (1)
    {
        cnt = cnt + 1;
        lv_tick_inc(1);
        //rt_kprintf("cnt = %d\n", cnt);
        //usleep(100);
        rt_thread_mdelay(1);
        // if(cnt % 1000 == 0)
        // {
        //     printf("cnt is %d\n",cnt);
        // }
    }
}

void lvgl_service(void *param)
{
    lv_init();
    fbdev_init();
    //rt_kprintf("test1\n");
    rtt_lcd_register();
    lv_port_indev_init();
    //rt_kprintf("test2\n");
    lv_demo_widgets();
    //rt_kprintf("test3\n");
    while (1)
    {
        rt_thread_mdelay(1);
        lv_task_handler();
    }
}

int main(void)
{
    rt_thread_t tid, tid2;

    tid2 = rt_thread_create("lvgl_tick",
                           lvgl_tick,
                           NULL,
                           4096,
                           8,
                           1);

    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);

    /* create thread for gui device */
    tid = rt_thread_create("lvgl",
                           lvgl_service,
                           NULL,
                           4096,
                           12,
                           10);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    while (1)
    {
        rt_thread_mdelay(10);
    }
    
    return 0;
}